﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services;

namespace MainWebService
{
    /// <summary>
    /// Summary description for CommonProxyService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class CommonService : System.Web.Services.WebService
    {
        [WebMethod]
        public List<string> AddDatabase(string dbName, bool isDataCaseSenitive, int loggedInUserId, byte[] data, List<int> associatedServices)
        {
            List<string> str = new List<string>();
            int newDBId = 0;
            string msgs = "";
            DatabaseDetails dbDetails = new DatabaseDetails();
            try
            {
                dbDetails.DBName = dbName;
                dbDetails.IsCaseSensitiveValidation = isDataCaseSenitive;
                dbDetails.CreatedBy = loggedInUserId;
                dbDetails.ServiceDetails = associatedServices;
                if (data != null && data.Length > 0)
                {
                    dbDetails.ScanDataList = dbDetails.GetScanDataList(data);
                }
                msgs = dbDetails.CreateDatabase(ref newDBId);

                str.Add(newDBId.ToString());
            }
            catch (Exception ex)
            {
                msgs = ex.Message;
            }
            str.Add(msgs);
            return str;
        }

        [WebMethod]
        public string DeleteDatabase(int dbId, int loggedInUserId)
        {
            string msgs = "";
            DatabaseDetails _dbDetails = new DatabaseDetails();
            try
            {
                _dbDetails.DbId = dbId;
                _dbDetails.CreatedBy = loggedInUserId;
                msgs = _dbDetails.DeteleDB();
            }
            catch (Exception ex)
            {
                Logger.LogText(ex.Message);
                msgs = ex.Message;
            }
            return msgs;
        }

        [WebMethod]
        public string RenameDatabase(int dbId, string newDBName, int loggedInUserId)
        {
            string msgs = "";
            DatabaseDetails dbDetails = new DatabaseDetails();
            try
            {
                dbDetails.DbId = dbId;
                dbDetails.DBName = newDBName;
                dbDetails.CreatedBy = loggedInUserId;
                msgs = dbDetails.RenameDB();
            }
            catch (Exception ex)
            {
                Logger.LogText(ex.Message);
                msgs = ex.Message;
            }
            return msgs;
        }

        [WebMethod]
        public string AssociateDBToService(int dbId, List<int> serviceId, int loggedInUserId)
        {
            string msgs = "";
            DatabaseDetails dbDetails = new DatabaseDetails();
            try
            {

                dbDetails.DbId = dbId;
                dbDetails.ServiceDetails = serviceId;
                dbDetails.CreatedBy = loggedInUserId;
                msgs = dbDetails.AssociateServiceToDb();
            }
            catch (Exception ex)
            {
                Logger.LogText(ex.Message);
                msgs = ex.Message;
            }
            return msgs;
        }

        [WebMethod]
        public DatabaseDetails GetDbDetail(int DbId, int loggedInUserId)
        {
            DatabaseDetails _dbDetails = new DatabaseDetails();
            try
            {
                _dbDetails.DbId = DbId;
                _dbDetails.CreatedBy = loggedInUserId;
                _dbDetails = _dbDetails.GetDatabaseDetails();
            }
            catch (Exception ex)
            {
                Logger.LogText(ex.Message);
                _dbDetails.UserMessage = ex.Message;
            }
            return _dbDetails;
        }

        [WebMethod]
        public List<DatabaseDetails> GetClientDBList(int clientId)
        {
            List<DatabaseDetails> lstDB = new List<DatabaseDetails>();
            DatabaseDetails _dbDetails = new DatabaseDetails();
            try
            {
                List<int> dbIds = new List<int>();
                dbIds = _dbDetails.GetDBIDList(clientId);
                for (int i = 0; i < dbIds.Count; i++)
                {
                    lstDB.Add(GetDbDetail(dbIds[i], clientId));
                }

            }
            catch (Exception ex)
            {
                Logger.LogText(ex.Message);
            }
            return lstDB;
        }

        [WebMethod]
        public string EmptyDatabase(int clientId,int dbId,string hashCode)
        {
            return new DatabaseDetails().EmptyDatabase(clientId, dbId, hashCode);
        }
        [WebMethod]
        public string ImportData(byte[] data, int dbId, int loggedInUserId)
        {
            string msgs = "";
            try
            {
                List<Scans> ScanDataList = null;
                DatabaseDetails dbDetails = new DatabaseDetails();
                if (data != null && data.Length > 0)
                {
                    ScanDataList = dbDetails.GetScanDataList(data);
                }
                if (ScanDataList!=null && ScanDataList.Count > 0)
                {
                    foreach (Scans item in ScanDataList)
                    {
                        msgs = AddScanData(dbId, loggedInUserId,item);
                        msgs = " " + msgs + " ";
                    }
                }

            }
            catch (Exception ex)
            {
                Logger.LogText(ex.Message);
                msgs = ex.Message;
            }

            return msgs;
        }

        [WebMethod]
        public byte[] ExportData(int dbId, int loggedInUserId)
        {
            try
            {


                DatabaseDetails _dbDetail = new DatabaseDetails();
                _dbDetail.DbId = dbId;
                _dbDetail.CreatedBy = loggedInUserId;
                string strScanData = _dbDetail.GetTableData();
                return Encoding.GetEncoding("iso-8859-1").GetBytes(strScanData);
            }
            catch (Exception ex)
            {
                Logger.LogText(ex.Message);
            }
            return null;
        }

        [WebMethod]
        public string AddScanData(int dbId, int loggedInUserId,Scans scanData)
        {
            string msgs = "";
            DatabaseDetails dbDetails = new DatabaseDetails();
            try
            {
                msgs = dbDetails.AddScanData(dbId,loggedInUserId,scanData);
            }
            catch (Exception ex)
            {
                Logger.LogText(ex.Message);
                msgs = ex.Message;
            }
            return msgs;
        }

        [WebMethod]
        public string EditScanData(int dbId, int loggedInUserId, string firstName, string lastName, int StudentIdNumber,int oldStudentId,int grade,int validity,string note1,string note2,string note3,string note4)
        {
            string msgs = "";
            DatabaseDetails dbDetails = new DatabaseDetails();
            try
            {
                ScanData _data = new ScanData();
                
                _data.FirstName = firstName;
                _data.LastName = lastName;
                _data.OldStudentIdNumber = oldStudentId;
                _data.StudentIdNumber = StudentIdNumber;
                _data.Grade = grade;
                _data.Validity = validity;
                _data.Note1 = note1;
                _data.Note2 = note2;
                _data.Note3 = note3;
                _data.Note4 = note4;
                dbDetails.DbId = dbId;
                dbDetails.CreatedBy = loggedInUserId;
                msgs = dbDetails.UpdateScanData(_data);
            }
            catch (Exception ex)
            {
                Logger.LogText(ex.Message);
                msgs = ex.Message;
            }
            return msgs;
        }

        [WebMethod]
        public string DeleteScanData(int dbId, int loggedInUserId, string studentIdNumber, string hashCode)
        {
            string msgs = "";
            DatabaseDetails dbDetails = new DatabaseDetails();
            try
            {
                dbDetails.DbId = dbId;
                dbDetails.CreatedBy = loggedInUserId;
                dbDetails.HashCode = hashCode;
                msgs = dbDetails.DeleteScanData(studentIdNumber);
            }
            catch (Exception ex)
            {
                Logger.LogText(ex.Message);
                msgs = ex.Message;
            }
            return msgs;
        }

        [WebMethod]
        public DataSet GetServices(int clientId, string hashCode)
        {
            try
            {
                ServiceDetails _service = new ServiceDetails();
                return _service.GetServiceList(clientId, hashCode);
            }
            catch (Exception ex)
            {
                Logger.LogText(ex.Message);
            }
            return null;
        }
        [WebMethod]
        public ServiceDetails GetServiceDetail(int serviceId,int userId,string hashCode)
        {
            return new ServiceDetails(serviceId, userId, hashCode);
        }

        [WebMethod]
        public List<ServiceDetails> GetServiceDetailsList(int clientId,string hashCode)
        {
            ServiceDetails _service = new ServiceDetails();
            return _service.GetServiceDetails(clientId, hashCode);
        }

        [WebMethod]
        public List<ServiceType> GetServiceType()
        {
            try
            {
                ServiceDetails _service = new ServiceDetails();
                return _service.GetServiceType();
            }
            catch (Exception ex)
            {
                Logger.LogText(ex.Message);
            }
            return null;
        }

        [WebMethod]
        public List<string> CreateRecordScansService(string serviceName, string serviceType, string serviceSubType, int userId, string hashCode,string serviceDes,string serviceDetail,string iconURL,string serviceURL)
        {
            string msg = "";
            List<string> lst = new List<string>();
            try
            {

                ServiceDetails _service = new ServiceDetails();
                int serviceId = 0;
                msg = _service.CreateService(serviceName, serviceType, serviceSubType, userId, hashCode,serviceDes,serviceDetail,iconURL,serviceURL,ref serviceId);
                lst.Add(serviceId.ToString());
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            lst.Add(msg);
            return lst;
        }

        [WebMethod]
        public string CreateValidateScansService(ValidateScan scan)
        {
            string msg = "";
            try
            {

                ServiceDetails _service = new ServiceDetails();
                msg = _service.CreateService(scan);
               
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return msg;
        }
        [WebMethod]
        public string CreateDBBuildernsService(string serviceName, string serviceType, string serviceSubType, int dbId, int userId, string hashCode)
        {
            return "";
        }
        [WebMethod]
        public string CreateThirdPartysService(string serviceName, string serviceType, string postBackURL, int userId, string hashCode)
        {
            return "";
        }
        [WebMethod]
        public string CreateWebViewService(string serviceName, string serviceType, int userId, string hashCode)
        {
            return "";
        }
        
        [WebMethod]
        public void UpdateSerice(int UserId,string HashCode,int ServiceId,string serviceName,string serviceDetail)
        {
            new ServiceDetails().UpdateService(UserId, HashCode, ServiceId, serviceName, serviceDetail);
        }


        [WebMethod]
        public string CreateQuestion(Questions objQues)
        {
            return new Questions().AddQuestion(objQues);
        }

        [WebMethod]
        public List<Questions> GetQuestionsList(int clientId, string hashCode)
        {
            return new Questions().GetQuestions(clientId, hashCode);
        }

        [WebMethod]
        public string AddServiceProperties(int serviceId,int clientId,ServiceProperties srvProperties)
        {
            return new ServiceOperations().AddServiceProperties(serviceId, clientId, srvProperties);
        }
        [WebMethod]
        public ServiceProperties GetServiceProperties(int clientId,int serviceId)
        {
            return new ServiceOperations().GetServiceProperties(serviceId, clientId);
        }
    }
    public class ValidateScan
    {
        public string serviceName{get;set;}
       public string serviceType{get;set;}
       public string serviceSubType { get; set; }
       public int userId { get; set; }
       public string hashCode { get; set; }
       public string databaseName { get; set; }
       public int dbId { get; set; }
       public bool duplicateScanAllowed { get; set; }
       public bool patterValidation { get; set; }
       public string pattern { get; set; }
       public string response { get; set; }
       public bool IsInvalidResForced { get; set; }
       public bool addValueWhenMatched { get; set; }
       public string serviceDes { get; set; }
       public string serviceDetail { get; set; }
       public string iconURL { get; set; }
       public string serviceURL { get; set; }
    }
}
